--
-- Author: ping.wang
-- Date: 2019-03-01 18:50:42
--
local fileName = string.getModeName(...)
local ClassRef = class(fileName)
local UserBaseInfo = require("app.public.dao.UserBaseInfo")
local PartnerHelper = require("app.partner.PartnerHelper")
local table_insert = table.insert

function ClassRef:ctor(data)
    if data then
        self:init(data)
    end
end

--[[
message memberInfo {
	base.UserBaseInfo userInfo = 1;
	int32 rankNum = 2; // 排名(0表示未上榜)
	int32 highestRanking = 3; // 历史最高排名
	int32 remainingTimes = 4; // 剩余挑战次数
	repeated partnerInfo onBattlePartners = 5; // 上阵随从ID
}
--]]
function ClassRef:init(data)
    self._memberUserBaseInfo = UserBaseInfo.new( data.userInfo )
    self._rankNum = rawget(data, "rankNum") or 0
    self._highestRanking = rawget(data, "highestRanking") or 0
    self._remainingTimes = rawget(data, "remainingTimes") or 0
    self._partnerInfos = {}
    --参战随从信息 id是string
    for _,v in ipairs(data.onBattlePartners) do
        table_insert(self._partnerInfos,{id = v.id, combatPower = v.combatPower, quality = PartnerHelper.getPartnerQualityByFightNum(v.combatPower)})
    end
    self._canRefresh = rawget(data, "canRefresh")
end

--当前排名
function ClassRef:getRankNum()
    return self._rankNum
end

--历史最高排名
function ClassRef:getHighestRanking()
    return self._highestRanking
end

--可以刷新次数
function ClassRef:getRemainingTimes()
    return self._remainingTimes
end

function ClassRef:getUserDataBaseInfo()
    return self._memberUserBaseInfo
end

function ClassRef:getPartnerInfos()
    return self._partnerInfos
end

function ClassRef:isMaxPartnerOnFight(max)
    local maxNum = max or 2
    return #self._partnerInfos >= maxNum
end

function ClassRef:isPartnerOn(id)
    for k,v in ipairs(self._partnerInfos) do
        if tonumber(id) == tonumber(v.id) then
            return true
        end
    end
    return false
end

--判断是否能刷新
function ClassRef:canRefresh()
    return self._canRefresh
end

function ClassRef:getTeamCombatPower()
    local cp = self._memberUserBaseInfo.combatPower
    for _,v in ipairs(self._partnerInfos) do
        cp = cp + v.combatPower
    end

    return cp
end

return ClassRef